# 登录需要
import hashlib
import json

# 接口测试需要
import requests

# ----------------------------------------- 登录 -------------------------------------------------------
# 构造请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36',
    'Content-Type': 'application/json;charset=utf-8'
}

# 构造url
base_url = r'https://cloud.huaxiaerp.com/'
url_login = base_url + '/jshERP-boot/user/login'

# 构造data
username = 'Missing'
password = 'h123456'
md5 = hashlib.md5(password.encode())  # 创建MD5对象
password_md5 = md5.hexdigest()  # 调用方法hexdigest加密
data_login = {
    "loginName": username,
    "password": password_md5
}
# 发送情求
resp = requests.post(url_login, json=data_login, headers=headers, verify=False)

# 获取响应
token = resp.json()['data']['token']
print(token)

# ----------------------------------------零售退货：新增----------------------------------------------------
# 构造操作headers_追加token
headers['X-Access-Token'] = token
# 构造url
url_retail_return_add = base_url + r'/jshERP-boot/depotHead/addDepotHeadAndDetail'
# 构造数据
# ---- 动态数据 ----
number = 'LSTH00000727754 '
linkNumber = 'LSCK00000727749'
accountId = 11902  # 账户名
id = 17050534866660885801
defaultNumber = 'LSCK00000726390'
null = ''
# 字典数据
info_dict = {"organId ": 142860, "operTime ": "2024-01-14 21:01:26 ", "number ": number, "linkNumber ": linkNumber,
             "changeAmount ": -500, "getAmount ": 500, "backAmount ": 0, "accountId ": 11902, "type ": "入库 ",
             "subType ": "零售退货 ", "defaultNumber ": number, "totalPrice ": -500, "fileName ": " ", "status ": "0 "}
info_str = json.dumps(info_dict)

rows_dict = [{"id ": "1454221 ", "barCode ": "1001 ", "name ": "眼影 ", "standard ": " ", "model ": " ", "color ": " ",
              "materialOther ": " ", "stock ": "75 ", "unit ": "盒 ", "snList ": " ", "batchNumber ": " ",
              "expirationDate ": " ", "sku ": " ", "preNumber ": "5 ", "finishNumber ": "0 ", "operNumber ": "5 ",
              "unitPrice ": "100 ", "allPrice ": "500 ", "remark ": " ", "linkId ": "1454221 ", "depotId ": "19957 "}]
rows_str = json.dumps(rows_dict)

data_retail_add = {
    'info': info_str,
    'rows': rows_str
}

# 发送请求
resp = requests.post(url_retail_return_add, headers=headers, json=data_retail_add, verify=False)
# 获取响应
print(resp.text)

# ----------------------------------------零售退出：修改----------------------------------------------------
# 构造 headers
headers['X-Access-Token'] = token
# 构造url
url_retail_mod = base_url + r'/jshERP-boot/depotHead/updateDepotHeadAndDetail'

# 构造数据
null = ''
number = 'LSTH00000727754'
linkNumber = 'LSCK00000727749'
info_dict = {
    "id ": 388340, "type ": "入库 ", "subType ": "零售退货 ", "defaultNumber ": number,
    "number ": number, "createTime ": "2024-01-14T13:01:43.000+0000 ", "operTime ": "2024-01-14 21:01:26 ",
    "organId ": 142860, "creator ": 151170, "accountId ": 11902, "changeAmount ": -300, "backAmount ": 0,
    "totalPrice ": -300, "payType ": "现付 ", "billType ": null, "remark ": null, "fileName ": " ", "salesMan ": null,
    "accountIdList ": null, "accountMoneyList ": null, "discount ": null, "discountMoney ": null,
    "discountLastMoney ": 0, "otherMoney ": null, "deposit ": 0, "status ": "0 ", "purchaseStatus ": "0 ",
    "source ": "0 ", "linkNumber ": linkNumber, "tenantId ": 151170, "deleteFlag ": "0 ", "projectName ": null,
    "organName ": "hy001 ", "userName ": null, "accountName ": "公司花西子收支账户 ", "allocationProjectName ": null,
    "materialsList ": "眼影    ", "salesManStr ": null, "operTimeStr ": "2024-01-14 21:01:26 ", "finishDebt ": null,
    "depotHeadType ": "零售退货入库 ", "creatorName ": "Missing ", "contacts ": null, "telephone ": null,
    "address ": null, "finishDeposit ": null, "needDebt ": null, "debt ": -500, "materialCount ": null,
    "hasFinancialFlag ": False, "hasBackFlag ": False, "realNeedDebt ": null, "getAmount ": "300.00 "

}
info_str = json.dumps(info_dict)
# 构造数据2
id = '1454228'
rows_dict = [{"id ": id, "barCode ": "1001 ", "name ": "眼影 ", "standard ": " ", "model ": " ", "color ": " ",
              "materialOther ": " ", "stock ": "80 ", "unit ": "盒 ", "snList ": " ", "batchNumber ": " ",
              "expirationDate ": " ", "sku ": " ", "preNumber ": "5 ", "finishNumber ": "0 ", "operNumber ": "3 ",
              "unitPrice ": "100 ", "allPrice ": 300, "remark ": " ", "linkId ": "1454221 ", "depotId ": "19957 "}]

rows_str = json.dumps(rows_dict)

data_retail_mod = {
    'info': info_str,
    'rows': rows_str
}

# 发送请求
resp = requests.put(url_retail_mod, headers=headers, json=data_retail_mod, verify=False)
# 获取响应
print(resp.text)

# ----------------------------------------零售退货：查询------------------------------------
# 构造url
url_retail_query = base_url + r'/jshERP-boot/depotHead/list'
# 构造headers
headers['X-Access-Token'] = token
# 构造数据
materialParam = '眼影',
number = ''
# 内层数据设置为字典

search_dict = {"number": number, "materialParam": materialParam, "type": "入库", "subType": "零售退货", "organId": "",
               "depotId": "", "creator": "", "linkNumber": "", "accountId": "", "status": "", "remark": ""}
# 将字典类型转换为字符串
search_str = json.dumps(search_dict)
data_retail_query = {
    'search': search_str,
    'column': 'createTime',
    'order': 'desc',
    'field': 'id,'',action,organName,number,materialsList,operTimeStr,userName,materialCount,totalPrice,getAmount,backAmount,status',
    'currentPage': 1,
    'pageSize': 10

}
# 发送请求
resp = requests.get(url_retail_query, params=data_retail_query, headers=headers, verify=False)
# 获取响应
print(resp.text)

# ------------------------------------零售退货： 删除--------------------------------------------------
# 构造 url
url_retail_delete = base_url + r'/jshERP-boot/depotHead/delete'
# 构造 headers
headers['X-Access-Token'] = token
# 构造数据
id = 388340
data_retail_delete = {
    'id': id
}
# 发送请求
resp = requests.delete(url_retail_delete, params=data_retail_delete, headers=headers, verify=False)
# 获取响应
print(resp.text)

